<?php

// Put your device token here (without spaces):
// Azad
//$deviceToken = '112cd2957efb30b925fd9c14a62c777f8b34230d0dab64729d961eb63e839596';
//$deviceToken = '74c07d1f0560f47fc71013e2641a05251263ab385ccd6760846691b56038e608';
// Sitara
//$deviceToken = '815209cf94248ed6241227a63cf08556afaa72ad54c8d7a99b29f16b31027536';
// Maalik
$deviceToken = 'b41436afba6763fa5e0c64ee5fa3143b45cdde7d3dea3a4af1413e569d392fff';
// Hasnene
//$deviceToken = '8d9a58e5119671a7b858e0bc3794e573b215c79559708bda6cea8b965027eded';

// Put your private key's passphrase here:
$passphrase = '78692110';

// Put your alert message here:
$message = 'Test';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
	'ssl://gateway.push.apple.com:2195', $err,
        //'ssl://gateway.sandbox.push.apple.com:2195', $err,
	$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
	exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
	'alert' => $message,
	'sound' => 'default',
        "badge" => 1
	);
$body['type'] = 'Programme';

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
	echo 'Message not delivered' . PHP_EOL;
else
	echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
